package com.wowangz.cms.systemmanage.databaseresource.service;


import java.text.MessageFormat;
import java.util.HashMap;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Strings;
import com.wowangz.cms.common.utils.SysConstant;
import com.wowangz.cms.syscore.exception.ServiceException;
import com.wowangz.cms.syscore.service.BaseService;
import com.wowangz.cms.systemmanage.databaseresource.pojo.DatabaseResource;


public class DatabaseResourceService extends BaseService {

	private Logger logger = LoggerFactory.getLogger(DatabaseResourceService.class);

	public void add(DatabaseResource databaseResource) throws ServiceException {
		this.getSqlSession().insert("DatabaseResource.insertDatabaseResource", databaseResource);
	}
	
	public void addResource(DatabaseResource databaseResource) throws Exception {
		long count = SysConstant.Dbtype.DB_COUNT;
		databaseResource.setUsedconn(0l);
		for (int i = 1; i <= count; i++) {
			databaseResource.setDbname(SysConstant.Dbtype.DB_PREFIX + MessageFormat.format("{0,number,00000}", new Object[]{new Long(i)}));
			this.getSqlSession().insert("DatabaseResource.insertDatabaseResource", databaseResource);
		}
	}
	
	public void delete(String ids) throws ServiceException {
		if (Strings.isNullOrEmpty(ids)) {
			return;
		}
		String[] temIds = ids.split(",");
		for (String id : temIds) {
			this.getSqlSession().delete("DatabaseResource.deleteDatabaseResource", id);
		}
	}

	public void update(DatabaseResource databaseResource) throws ServiceException {
		this.getSqlSession().update("DatabaseResource.updateDatabaseResource", databaseResource);
	}

	public DatabaseResource findDatabaseResourceById(String id) throws ServiceException {
		DatabaseResource databaseResource = this.getSqlSession().selectOne("DatabaseResource.findDatabaseResourceById", id);
		return databaseResource;
	}

	public List<DatabaseResource> query(DatabaseResource databaseResource) throws ServiceException {
		List<DatabaseResource> list = this.getSqlSession().selectList("DatabaseResource.queryDatabaseResource");
		return list;
	}

	/**
	 * 分页查询总条数
	 * 
	 * @param map
	 * @return
	 */
	public long findResultCount(HashMap<String, Object> map) throws ServiceException {
		Long countNum = this.getSqlSession().selectOne("DatabaseResource.queryDatabaseResourceCount", map);
		return countNum;
	}

	/**
	 * 查询记录集合
	 * 
	 * @param map
	 * @return
	 */
	public List<DatabaseResource> findResultList(HashMap<String, Object> map) throws ServiceException {
		List<DatabaseResource> list = this.getSqlSession().selectList("DatabaseResource.queryDatabaseResourcePage", map);
		return list;
	}
	
	public boolean valiDatabaseResource(HashMap<String, Object> map){
		Long count = this.getSqlSession().selectOne("DatabaseResource.valiDatabaseResource", map);
		if(count == null || count == 0){
			return false;
		}else{
			return true;
		}
	}


}
